<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从 FTP 服务器上获取文件并写入本地文件（non-blocking）</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.ftp-nb-get" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">ftp_nb_get</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">ftp_nb_get</span> &mdash; <span class="dc-title">从 FTP 服务器上获取文件并写入本地文件（non-blocking）</span></p>

   </div>
   <div class="refsect1 unknown-seealsp" id="refsect1-function.ftp-nb-get-unknown-seealsp">
    <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>ftp_nb_get</strong></span>
      ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$ftp_stream</code></span>
     , <span class="methodparam"><span class="type">string</span> <code class="parameter">$local_file</code></span>
     , <span class="methodparam"><span class="type">string</span> <code class="parameter">$remote_file</code></span>
     , <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code></span>
     [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$resumepos</code></span>
    ] ) : <span class="type">bool</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>ftp_nb_get()</strong></span> 函数用来获取参数 <code class="parameter">remote_file</code>
     指定的的远程文件，并保存到由参数 <code class="parameter">local_file</code>
     指定的本地文件。传输模式参数
     <code class="parameter">mode</code> 只能为 <strong><code>FTP_ASCII</code></strong> (文本模式) 或 <strong><code>FTP_BINARY</code></strong>
     (二进制模式) 两种。与 <span class="function"><a href="ftp_get.html" class="function">ftp_get()</a></span>
     函数不同的是，此函数上传文件的时候采用的是异步传输模式，也就意味着在文件传送的过程中，你的程序可以继续干其它的事情。
    </p>
    <p class="para">
     返回 <strong><code>FTP_FAILED</code></strong>，<strong><code>FTP_FINISHED</code></strong>
     或 <strong><code>FTP_MOREDATA</code></strong>。
    </p>
    <p class="para">
     <div class="example" id="example-5320">
      <p><strong>Example #1 <span class="function"><strong>ftp_nb_get()</strong></span> 实例</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;开始下载<br /></span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ftp_nb_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$my_connection</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"README"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FTP_BINARY</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">FTP_MOREDATA</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;这里可以插入其它代码<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"."</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;继续下载...<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ftp_nb_continue&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$my_connection</span><span style="color: #007700">);<br />}<br />if&nbsp;(</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">FTP_FINISHED</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"下载中出错..."</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
     <div class="example" id="example-5321">
      <p><strong>Example #2 使用 <span class="function"><strong>ftp_nb_get()</strong></span> 函数断线续传</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;开始<br /></span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ftp_nb_get&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$my_connection</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"README"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FTP_BINARY</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">//&nbsp;或:&nbsp;$ret&nbsp;=&nbsp;ftp_nb_get&nbsp;($my_connection,&nbsp;"test",&nbsp;"README",<br />//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FTP_BINARY,&nbsp;FTP_AUTORESUME);<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">FTP_MOREDATA</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;可以插入其它代码<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"."</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;继续传送...<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ftp_nb_continue&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$my_connection</span><span style="color: #007700">);<br />}<br />if&nbsp;(</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">FTP_FINISHED</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"下载出错..."</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
     <div class="example" id="example-5322">
      <p><strong>Example #3 
       用 <span class="function"><strong>ftp_nb_get()</strong></span> 在 100 的位置断线续传并存为 &quot;newfile&quot;
      </strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
//&nbsp;禁止自动搜寻<br />ftp_set_option&nbsp;($my_connection,&nbsp;FTP_AUTOSEEK,&nbsp;FALSE);<br /><br />//&nbsp;开始<br />$ret&nbsp;=&nbsp;ftp_nb_get&nbsp;($my_connection,&nbsp;"newfile",&nbsp;"README",&nbsp;FTP_BINARY,&nbsp;100);<br />while&nbsp;($ret&nbsp;==&nbsp;FTP_MOREDATA)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;...<br /><br />&nbsp;&nbsp;&nbsp;//&nbsp;继续下载...<br />&nbsp;&nbsp;&nbsp;$ret&nbsp;=&nbsp;ftp_nb_continue&nbsp;($my_connection);<br />}</span>
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     在上边的例子中，<var class="filename">&quot;newfile&quot;</var> 文件比服务器上的文件
     <var class="filename">&quot;README&quot;</var> 要小 100 字节。这是因为我们是从文件的偏移量 100
     处开始读取的，如果没有禁止 <strong><code>FTP_AUTOSEEK</code></strong>，则
     <var class="filename">&quot;newfile&quot;</var> 的前 100
     字节将会是 <em>&#039;\0&#039;</em>。
    </p>
    <p class="para">
     参见 <span class="function"><a href="ftp_nb_fget.html" class="function">ftp_nb_fget()</a></span>，<span class="function"><a href="ftp_nb_continue.html" class="function">ftp_nb_continue()</a></span>，<span class="function"><a href="ftp_get.html" class="function">ftp_get()</a></span>
     和 <span class="function"><a href="ftp_fget.html" class="function">ftp_fget()</a></span>。
    </p>
   </div>

  </div></div></div></body></html>